package com.its.us.web.controller.sys.base;

import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: master
 * @Date: 2020/11/14 15:18
 * @Version: 1.0
 */
public abstract class BaseWebPaginationCURDController extends WebCRUDBaseController {

    protected Map<String, Object> initPaginationParam(
            MultiValueMap<String, String> param) {
        if (param == null || param.isEmpty()) {
            return null;
        } else {
            Map<String, Object> result = new HashMap<String, Object>();
            for (String key : param.keySet()) {
                result.put(key, param.getFirst(key));
            }
            if (param.containsKey("page") && param.containsKey("rows")) {
                result.put("page",
                        (Integer.parseInt(param.getFirst("page")) - 1)
                                * Integer.parseInt(param.getFirst("rows")));
            }
            return result;
        }
    }

    protected Map<String, Object> initParam(
            MultiValueMap<String, String> param) {
        if (param == null || param.isEmpty()) {
            return null;
        } else {
            Map<String, Object> result = new HashMap<>();
            for (String key : param.keySet()) {
                result.put(key, param.getFirst(key));
            }
            return result;
        }
    }
}
